
import { InputType } from '../network/net_proto/protocols/common';
import { App } from '../app';
import { NetInfo } from '../core/NetInfo';
import { AudioName } from '../../Frame/Script/const/enumConst';
const { ccclass, property } = cc._decorator;

@ccclass
export class InputManager extends cc.Component {

    @property(cc.Node)
    btnLeft: cc.Node = null;
    @property(cc.Node)
    btnRight: cc.Node = null;
    @property(cc.Node)
    btnDown: cc.Node = null;
    @property(cc.Node)
    btnRotate: cc.Node = null;
    @property(cc.Node)
    btnDrop: cc.Node = null;

    private readonly delaytime: number = 1000;
    private readonly interval: number = 100;

    protected onLoad(): void {
        this.btnLeft.on(cc.Node.EventType.TOUCH_START, this.onLeftTouch, this);
        this.btnRight.on(cc.Node.EventType.TOUCH_START, this.onRightTouch, this);
        this.btnDown.on(cc.Node.EventType.TOUCH_START, this.onDownTouch, this);
        this.btnRotate.on(cc.Node.EventType.TOUCH_START, this.onRotateTouch, this);
        this.btnDrop.on(cc.Node.EventType.TOUCH_START, this.onDropTouch, this);

        this.btnLeft.on(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnRight.on(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnDown.on(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnRotate.on(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnDrop.on(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);

        this.btnLeft.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnRight.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnDown.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnRotate.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnDrop.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    protected onDestroy(): void {
        this.btnLeft.off(cc.Node.EventType.TOUCH_START, this.onLeftTouch, this);
        this.btnRight.off(cc.Node.EventType.TOUCH_START, this.onRightTouch, this);
        this.btnDown.off(cc.Node.EventType.TOUCH_START, this.onDownTouch, this);
        this.btnRotate.off(cc.Node.EventType.TOUCH_START, this.onRotateTouch, this);
        this.btnDrop.off(cc.Node.EventType.TOUCH_START, this.onDropTouch, this);

        this.btnLeft.off(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnRight.off(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnDown.off(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnRotate.off(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);
        this.btnDrop.off(cc.Node.EventType.TOUCH_END, this.onBtnLift, this);

        this.btnLeft.off(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnRight.off(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnDown.off(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnRotate.off(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
        this.btnDrop.off(cc.Node.EventType.TOUCH_CANCEL, this.onBtnLift, this);
    }

    /**
    * 输入按钮回调事件
    * 1：左
    * 2：右
    * 3：下（加速）
    * 4：转
    * 5：落定
    */

    private onLeftTouch(e) {
        this.emitInput(e, InputType.left);
    }

    private onRightTouch(e) {
        this.emitInput(e, InputType.right);
    }

    private onDownTouch(e) {
        this.emitInput(e, InputType.down);
    }

    private onRotateTouch(e) {
        this.emitInput(e, InputType.rotate);
    }

    private onDropTouch(e) {
        this.emitInput(e, InputType.drop);
    }

    private async onBtnLift(e) {
        if (!App.gameData.isGameStart || App.gameData.isGameOver) return;
        if (e.target.name === "btn_down") {
            await App.ws.callApi('game/PlayerInput', { input: InputType.down_lift, session: NetInfo.session });
        }
    }

    private async emitInput(e, type: InputType) {
        if (!App.gameData.isGameStart || App.gameData.isGameOver) return;
        let reslut = await App.ws.callApi('game/PlayerInput', { input: type, session: NetInfo.session });
        if (reslut.isSucc) {
            switch (type) {
                case InputType.drop:
                    App.audio.playEffect(AudioName.quick_drop);
                    break;
                case InputType.rotate:
                    App.audio.playEffect(AudioName.change_model_lrd)
                    break;
                default:
                    break;
            }
        }
    }
}


